// pages/businessCircleDetail/businessCircleDetail.js
const app = getApp();
import httpsUtils from '../../utils/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    window_height: 0,
    navbar: ['热门', "最新"],
    //count:[0,2,3],                               
    currentTab: 0,
    isFocus: false,
    noFocus: true,
    current: 1,
    hasNextPage: true,
    bussinessGoodsList: [],
    bussinessGoodsLength: 1,

    currentNew: 1,
    hasNextPageNew: true,
    bussinessNewList: [],
    bussinessNewLength: 1,

    tradeAreaId:''

  },
  /**
 * 跳转首页
 */
  toindex: function () {
    wx.switchTab({
      url: '../index/index',
    })
  },
   /**
     * 商圈列表请求
     * sort=1 表示热门
     * sort=2 表示最新
     */
  selectShopList:function(sort){
    var that = this;
    var current = 1;
    if (sort==1){
      current = that.data.current
    }else{
      current = that.data.currentNew
    }
    httpsUtils.getData({
      _url: app.globalData._url + '/tradeArea/selectTradeGoodsList?sort=' + sort + "&current=" + current + "&tradeAreaId=" + that.data.tradeAreaId,
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        if(sort == 1){
          that.setData({
            bussinessGoodsList: that.data.bussinessGoodsList.concat(json.data.list),
            bussinessGoodsLength: json.data.total,
            current: json.data.pageNum,
            hasNextPage: json.data.hasNextPage,
          })
        }else{
          that.setData({
            bussinessNewList: that.data.bussinessNewList.concat(json.data.list),
            bussinessNewLength: json.data.total,
            currentNew: json.data.pageNum,
            hasNextPageNew: json.data.hasNextPage,
          })
        }
       
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var wHeight = res.windowHeight * 2;
        that.setData({
          window_height: wHeight,
        })
      }
    })
    that.setData({
      tradeAreaId: options.id,
    })
    /* 热门商圈店面 */
    that.selectShopList(1);
    /* 最新商圈店面 */
    that.selectShopList(2);
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      isFocus: false,
      noFocus: true,
      current: 1,
      hasNextPage: true,
      bussinessGoodsList: [],
      bussinessGoodsLength: 1,

      currentNew: 1,
      hasNextPageNew: true,
      bussinessNewList: [],
      bussinessNewLength: 1,

    })
    /* 热门商圈店面 */
    that.selectShopList(1);
    /* 最新商圈店面 */
    that.selectShopList(2);

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.currentTab == 0) {
      if (that.data.hasNextPage) {
        that.setData({
          current: that.data.current + 1,
        })
        that.selectShopList(1);
      }
    } else {
      if (that.data.hasNextPageNew) {
        that.setData({
          currentNew: that.data.currentNew + 1,
        })
        that.selectShopList(2);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (this.data.bussinessGoodsList.size>0){
      return {
        title: this.data.bussinessGoodsList[0].tradeArea.areaName,
        path: '/pages/businessCircleDetail/businessCircleDetail?id=' + this.data.tradeAreaId,
      }
    }else{
      return {
        title: '服装基地-商圈',
        path: '/pages/businessCircleDetail/businessCircleDetail?id=' + this.data.tradeAreaId,
      }
    }
   
  },
  
  /*关注
   focus: function(e){
     var that = this;
    that.setData({
       isFocus: true,
       noFocus: false,
     })
   },
   */
  /**
   * 商圈详情导航
   */
  navbarTap: function (e) {
    var that = this;
    that.setData({
      currentTab: e.currentTarget.dataset.idx,
      TypeItem: that.data.navbar[that.data.currentTab]
    })
  },
  /* 数据请求 */
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
})